package com.ptszyxx.popose.module.main.user.vm;

import android.app.Application;

import androidx.annotation.NonNull;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.EditEnum;
import com.ptszyxx.popose.common.event.EditEvent;
import com.ptszyxx.popose.common.event.LabelEvent;
import com.ptszyxx.popose.common.oos.YOosUtil;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ptszyxx.popose.common.utils.YBusUtil;
import com.ysg.utils.YUserUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.RxBus;
import com.ysg.bus.RxSubscriptions;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseListResponse;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.user.AreaEntity;
import com.ysg.http.data.entity.user.CityEntity;
import com.ysg.http.data.entity.user.ProvinceEntity;
import com.ysg.http.data.entity.user.UserAddressResult;
import com.ysg.http.data.entity.user.UserHeightEntity;
import com.ysg.http.data.entity.user.UserResult;
import com.ysg.http.data.entity.user.UserWorkEntity;
import com.ysg.http.data.entity.user.UserWorkItemEntity;
import com.ysg.utils.YToastUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class UserEditVM extends BaseViewModel<CommonRepository> {
    public String userId;
    public UserResult entity;
    public List<UserHeightEntity> heightList;
    public List<UserHeightEntity> weightList;
    public List<UserHeightEntity> incomeList;
    public List<UserHeightEntity> educationList;
    public List<UserWorkEntity> workList;
    public UserAddressResult addressResult;
    public String voiceUrl;
    public boolean updateFlag;//是否有修改用户标记

    public UserEditVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onSelectAvatarEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onAvatarUIEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onBirthdayEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onHeightEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onWeightEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onIncomeEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onEducationEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onWorkEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onAddressEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onUIEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onRefreshPicEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onRefreshAddPicEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onAvatarCommand = new BindingCommand(()-> uc.onSelectAvatarEvent.call());
    public BindingCommand onNickCommand = new BindingCommand(()->jumpUserEditName(EditEnum.name));
    public BindingCommand onSignCommand = new BindingCommand(()->jumpUserEditName(EditEnum.sign));
    public BindingCommand onBirthdayCommand = new BindingCommand(()->uc.onBirthdayEvent.call());
    public BindingCommand onHeightCommand = new BindingCommand(()->uc.onHeightEvent.call());
    public BindingCommand onWeightCommand = new BindingCommand(()->uc.onWeightEvent.call());
    public BindingCommand onIncomeCommand = new BindingCommand(()->uc.onIncomeEvent.call());
    public BindingCommand onEducationCommand = new BindingCommand(()->uc.onEducationEvent.call());
    public BindingCommand onWorkCommand = new BindingCommand(()->uc.onWorkEvent.call());
    public BindingCommand onAddressCommand = new BindingCommand(()->uc.onAddressEvent.call());
    public BindingCommand onLabelCommand = new BindingCommand(()->jumpUserEditLabel());
    public BindingCommand onVoiceCommand = new BindingCommand(()->YActivityUtil.getInstance().jumpUserEditVoice(this));

    public Disposable disposableNick,disposableLabel;
    @Override
    public void registerRxBus() {
        super.registerRxBus();
        disposableNick = RxBus.getDefault().toObservable(EditEvent.class)
                .subscribe(new Consumer<EditEvent>() {
                    @Override
                    public void accept(EditEvent event){
                        if (event != null && event.getEditEnum() != null){
                            if (EditEnum.name == event.getEditEnum()){
                                entity.setNick(event.getContent());
                            }else if (EditEnum.sign == event.getEditEnum()){
                                entity.setMysign(event.getContent());
                            }
                            uc.onUIEvent.call();
                            updateFlag = true;
                        }
                    }
                });
        disposableLabel = RxBus.getDefault().toObservable(LabelEvent.class)
                .subscribe(new Consumer<LabelEvent>() {
                    @Override
                    public void accept(LabelEvent event){
                        if (event != null){
                            entity.setBiaoqianname(event.getNames());
                            uc.onUIEvent.call();
                            updateFlag = true;
                        }
                    }
                });
    }

    @Override
    public void removeRxBus() {
        super.removeRxBus();
        RxSubscriptions.remove(disposableNick);
        RxSubscriptions.remove(disposableLabel);
    }

    /**
     * 编辑用户昵称
     */
    private void jumpUserEditName(EditEnum editEnum) {
        String content = "";
        if (EditEnum.name == editEnum){
            content = entity.getNick();
        }else if (EditEnum.sign == editEnum){
            content = entity.getMysign();
        }
        YActivityUtil.getInstance().jumpUserEditName(this, userId, content, editEnum);
    }
    /**
     * 编辑用户标签
     */
    private void jumpUserEditLabel() {
        YActivityUtil.getInstance().jumpUserEditLabel(this);
    }

    /**
     * 请求用户信息
     */
    public void requestUser(){
        Map<String, String> map = new HashMap<>();

        HttpUtils.getInstance()
                .data(model.user(map), this,
                        new OnSuccessResult<UserResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<UserResult> response) {
                                entity = response.getData();
                                uc.onAvatarUIEvent.call();
                                uc.onRefreshPicEvent.call();
                                uc.onUIEvent.call();
                            }
                        });
    }

    public void requestOosToken(){
        YOosUtil.getInstance().getOosToken(model, this);
    }

    public void requestOosUploadPic(String path, boolean isAvatar) {
        YOosUtil.getInstance().upload(path, userId, true, new YOosUtil.OnOosListener() {
            @Override
            public void onSuccess(String url) {
                if (isAvatar){
                    requestUploadAvatar(url);
                }
            }

            @Override
            public void onSuccess(List<String> list) {
            }

            @Override
            public void onError() {
                YToastUtil.showShort(R.string.fail);
            }
        });
    }

    /**
     * 请求用户信息
     */
    public void requestUploadAvatar(String url){
        Map<String, String> map = new HashMap<>();
        map.put("pic", url);
        HttpUtils.getInstance()
                .data(model.userEditAvatar(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                entity.setPic(url);
                                uc.onAvatarUIEvent.call();
                                updateFlag = true;
                                YUserUtil.saveAvatar(url);
                            }
                        });
    }

    /**
     * 上传形象
     */
    public void requestUploadXingxiang(String path){
        File file = new File(path);
        if (file == null){
            return;
        }
        HttpUtils.getInstance()
                .data(model.userEditXingxiang(file), this,
                        new OnSuccessResult<String>() {
                            @Override
                            public void onSuccessResult(BaseResponse<String> response) {
                                uc.onRefreshAddPicEvent.setValue(response.getData());
                                updateFlag = true;
                            }
                        });
    }

    /**
     * 上传形象
     */
    public void requestXingxiangDelete(String pic){
        Map<String, String> map = new HashMap<>();
        map.put("pic", pic);
        HttpUtils.getInstance()
                .data(model.userEditXingxiangDelete(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                updateFlag = true;
                            }
                        });
    }

    /**
     * 生日
     */
    public void requestBirthday(String birthday){
        Map<String, String> map = new HashMap<>();
        map.put("birthday", birthday);
        HttpUtils.getInstance()
                .data(model.userEditBirthday(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                entity.setBirthday(birthday);
                                uc.onUIEvent.call();
                                updateFlag = true;
                            }
                        });
    }

    /**
     * 身高
     */
    public void requestHeight(int type, UserHeightEntity heightEntity){
        Map<String, String> map = new HashMap<>();
        Observable observable = model.userEditHeight(map);;
        switch (type){
            case 1:
                map.put("shengao", heightEntity.getId()+"");
                observable = model.userEditHeight(map);
                break;
            case 2:
                map.put("tizhong", heightEntity.getId()+"");
                observable = model.userEditWeight(map);
                break;
            case 3:
                map.put("nianshouru", heightEntity.getId()+"");
                observable = model.userEditIncome(map);
                break;
            case 4:
                map.put("xueliru", heightEntity.getId()+"");
                observable = model.userEditEducation(map);
                break;
        }

        HttpUtils.getInstance()
                .data(observable, this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                switch (type){
                                    case 1:
                                        entity.setShengao(heightEntity.getValue());
                                        break;
                                    case 2:
                                        entity.setTizhong(heightEntity.getValue());
                                        break;
                                    case 3:
                                        entity.setNianshouru(heightEntity.getValue());
                                        break;
                                    case 4:
                                        entity.setEduStatusRu(heightEntity.getValue());
                                        break;
                                }
                                uc.onUIEvent.call();
                                updateFlag = true;
                            }
                        });
    }

    /**
     * 生日
     */
    public void requestWork(UserWorkItemEntity itemEntity){
        Map<String, String> map = new HashMap<>();
        map.put("zhiyecode", ""+itemEntity.getCode());
        HttpUtils.getInstance()
                .data(model.userEditWork(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                entity.setZhiyecode(itemEntity.getCode());
                                entity.setZhiyename(itemEntity.getName());
                                uc.onUIEvent.call();
                            }
                        });
    }

    /**
     * 生日
     */
    public void requestAddress(ProvinceEntity province, CityEntity city, AreaEntity area){
        Map<String, String> map = new HashMap<>();
        if (province != null){
            map.put("provinceid", province.getProvinceid() + "");
        }
        if (city != null){
            map.put("cityid", city.getCityid() + "");
        }
        if (area != null){
            map.put("areaId", area.getId() + "");
        }
        HttpUtils.getInstance()
                .data(model.userEditAddress(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                if (city != null){
                                    entity.setAddress(city.getCity());
                                }
                                uc.onUIEvent.call();
                            }
                        });
    }


    /**
     * 身高
     */
    public void requestHeightList(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .list(model.userEditHeightList(map), this,
                        new OnSuccessListResult<UserHeightEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<UserHeightEntity> response) {
                                heightList = response.getData();
                            }
                        });
    }

    /**
     * 身高
     */
    public void requestWeightList(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .list(model.userEditWeightList(map), this,
                        new OnSuccessListResult<UserHeightEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<UserHeightEntity> response) {
                                weightList = response.getData();
                            }
                        });
    }
    /**
     * 身高
     */
    public void requestIncomeList(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .list(model.userEditIncomeList(map), this,
                        new OnSuccessListResult<UserHeightEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<UserHeightEntity> response) {
                                incomeList = response.getData();
                            }
                        });
    }
    /**
     * 身高
     */
    public void requestEducationList(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .list(model.userEditEducationList(map), this,
                        new OnSuccessListResult<UserHeightEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<UserHeightEntity> response) {
                                educationList = response.getData();
                            }
                        });
    }

    /**
     * 身高
     */
    public void requestWorkList(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .list(model.userEditWorkList(map), this,
                        new OnSuccessListResult<UserWorkEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<UserWorkEntity> response) {
                                workList = response.getData();
                            }
                        });
    }

    /**
     * 身高
     */
    public void requestAddressList(){
        Map<String, String> map = new HashMap<>();
        HttpUtils.getInstance()
                .data(model.userEditAddressList(map), this,
                        new OnSuccessResult<UserAddressResult>() {
                            @Override
                            public void onSuccessResult(BaseResponse<UserAddressResult> response) {
                                addressResult = response.getData();
                            }
                        });
    }

    public void updateBirthday(String birthday){
        requestBirthday(birthday);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (updateFlag){
            YBusUtil.userEdit();
        }
    }
}
